/**
 * MonitorWorkBenchAction.java
 * Created at 2017-3-22
 * Created by xieshuang
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.monitor.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.utils.BaiduAPI;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.rms.basic.service.ITmCustomService;
import com.broadtext.rms.monitor.model.PackAlertView;
import com.broadtext.rms.monitor.model.PackLocationInfo;
import com.broadtext.rms.monitor.model.PlantPackTree;
import com.broadtext.rms.monitor.service.IMonitorWorkBenchService;
import com.broadtext.rms.monitor.service.IPackAlertService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: MonitorWorkBenchAction</p>
 * <p>Description: 监控员控制台控制层</p>
 * <p>Author: xieshuang</p>
 * <p>Date: 2017-3-22</p>
 */
@Controller
@RequestMapping("/monitor/workbench")
public class MonitorWorkBenchAction {
    /**
     * 日志信息
     */
    public static Logger logger = LoggerFactory.getLogger(MonitorWorkBenchAction.class);
    
    /**
     * 服务类
     */
    @Autowired
    @Qualifier("monitorWorkBenchService")
    private IMonitorWorkBenchService monitorWorkBenchService;
    
    /**
     * 电池报警信息服务类
     */
    @Autowired
    @Qualifier("packAlertService")
    private IPackAlertService packAlertService;
    
    /**
     * o 注入客户service
     */
    @Autowired
    @Qualifier("tmCustomService")
    private ITmCustomService moniotrTmCustomService;
    
    /**
     * 
     * <p>Description: 获取客户下电池使用情况</p>
     * @param pkId 客户住家主键id
     * @return 结果集
     * @throws DaoException 异常
     */
    @RequestMapping(value="getCustomPackAlertInfo.mvc")
    @ResponseBody
    public Object getCustomPackAlertInfo(@RequestParam("cusId")String pkId) throws DaoException{
        //存储主键id
        Map<String, Object> packAlertInfo;
        packAlertInfo = new HashMap<String, Object>();
        packAlertInfo = this.monitorWorkBenchService.findCustomPackAlertInfo(pkId);
        return packAlertInfo;
    }
    
    /**
     * 
     * <p>Description: 获取报警列表</p>
     * @param request 请求
     * @return 结果集
     * @throws DaoException 异常
     */
    @RequestMapping(value="getPackAlertInfoList.mvc")
    @ResponseBody
    public Object getPackAlertInfoList(HttpServletRequest request){
        //处理查询条件
        Map<String, Object> map;
        String aoData;
        aoData = request.getParameter("aoData");
        JSONArray array;
        array = JSON.parseArray(aoData);
        map = new HashMap<String, Object>();
        if (array != null) {
            Iterator<Object> it;
            it = array.iterator();
            while (it.hasNext()) {
                JSONObject obj;
                obj = (JSONObject) it.next();
                if (obj.get("value") != null && StringUtils.isNotBlank(obj.get("value")+"")) {
                    map.put(obj.get("name") + "", obj.get("value"));
                }
            }
        }
        //查询
        List<PackAlertView> alertList;
        alertList = new ArrayList<PackAlertView>();
        try {
            alertList = this.monitorWorkBenchService.findPackAlertInfoList(map);
        } catch (DaoException e) {
            logger.error("获取报警列表出错：", e);
            e.printStackTrace();
        }
        Map<String, Object> dataMap;
        dataMap = new HashMap<String, Object>();
        dataMap.put("aaData",alertList);
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: 获取报警信息分页列表</p>
     * @param request 请求
     * @return 结果集
     * @throws DaoException 异常
     */
    @RequestMapping(value="getPackAlertInfoList1.mvc")
    @ResponseBody
    public Object getPackAlertInfoList1(HttpServletRequest request){
        PageBounds pageBounds = new PageBounds();
        //处理查询条件
        Map<String, Object> map;
        String aoData;
        aoData = request.getParameter("aoData");
        JSONArray array;
        array = JSON.parseArray(aoData);
        map = new HashMap<String, Object>();
        if (array != null) {
            Iterator<Object> it;
            it = array.iterator();
            while (it.hasNext()) {
                JSONObject obj;
                obj = (JSONObject) it.next();
                if (obj.get("value") != null && StringUtils.isNotBlank(obj.get("value")+"")) {
                    map.put(obj.get("name") + "", obj.get("value"));
                }
            }
        }
      //分页查询条数
        pageBounds.setLimit(Integer.parseInt(map.get("iDisplayLength").toString()));
        //分页第几页
        pageBounds.setPage(Integer.parseInt(map.get("iDisplayStart").toString()) / pageBounds.getLimit() + 1);
        Map<String, Object> dataMap = new HashMap<String, Object>();
        try {
            EntityPageBean<PackAlertView> pageBean = this.packAlertService.findEntityPage(map, pageBounds);
            dataMap.put("recordsTotal", pageBean.getiTotalRecords());
            dataMap.put("recordsFiltered", pageBean.getiTotalRecords());
            dataMap.put("aaData", pageBean.getResults());
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return dataMap;
    }
    
    
    /**
     * 
     * <p>Description: 获取plant_pack树</p>
     * @param cusId 客户主键id
     * @return 结果集
     * @throws DaoException 异常
     */
    @RequestMapping(value="getPlantPackTree.mvc")
    @ResponseBody
    public Object getPlantPackTree(@RequestParam("customerId")String cusId){
        //存储主键id
        List<PlantPackTree> treeList;
        treeList = new ArrayList<PlantPackTree>();
        try {
            treeList = this.monitorWorkBenchService.findPlantPackTreeByCustomer(cusId);
        } catch (DaoException e) {
            logger.error("获取plant_pack树：", e);
            e.printStackTrace();
        }
        return treeList;
    }
    
    
    /**
     * 
     * <p>Description: 获取电池位置信息</p>
     * @param cusId 客户主键id
     * @param packCodes 电池id
     * @return 结果集
     * @throws DaoException 异常
     */
    @RequestMapping(value="getPackLocationInfo.mvc")
    @ResponseBody
    public Object getPackLocationInfo(@RequestParam("packCodes")String packCodes, @RequestParam("cusId")String cusId){
    	Map<String, Object> queryMap = new HashMap<String, Object>();//查询条件
        List<PackLocationInfo> locationList;
        locationList = new ArrayList<PackLocationInfo>();
        try {
        	if(!"".equals(packCodes)){
        		queryMap.put("queryParams", packCodes);
        		queryMap.put("flag", "2");
        	} else {
        		String[] packCodesArray = null;
        		List<PlantPackTree> treeList;
                treeList = new ArrayList<PlantPackTree>();
                treeList = this.monitorWorkBenchService.findPlantPackTreeByCustomer(cusId);
                if(treeList.size() != 0){
                	packCodesArray = new String[treeList.size()];
                	for (int i = 0; i < treeList.size(); i++) {
                		packCodesArray[i] = treeList.get(i).getName();
					}
                }
                queryMap.put("flag", "1");
                queryMap.put("queryParams", packCodesArray);
        	}
            locationList = this.monitorWorkBenchService.findPackLocationInfo(queryMap);
        } catch (DaoException e) {
            logger.error("获取电池位置信息出错：", e);
            e.printStackTrace();
        }
        return locationList;
    }
    
    /**
     * 
     * <p>Description: 获取客户字典</p>
     * @param cusName 客户名称
     * @param request 请求对象
     * @param response 返回对象
     * @throws DaoException 异常
     */
    @RequestMapping(value = "/getAlertTableQuerySelectOption.mvc")
    @ResponseBody
    public Object getCustomDict(String cusName, HttpServletRequest request, HttpServletResponse response)
            throws DaoException {
        Map<String, Object> queryMap = new HashMap<String, Object>();
        //客户
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        list = this.moniotrTmCustomService.getCustomDict(queryMap);
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("customers", list);
        
        //报警类型
        List<Map<String, String>> alertNameList = new ArrayList<Map<String, String>>(); 
        alertNameList = this.monitorWorkBenchService.getAlertNameList(".getAlertNameList");
        dataMap.put("alertNameList", alertNameList);
        //报警类别
        List<Map<String, String>> alertLevelList = new ArrayList<Map<String, String>>();
        alertLevelList = this.monitorWorkBenchService.getAlertNameList(".getAlertLevelList");
        dataMap.put("alertLevelList", alertLevelList);
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: 获取概览页面需要数据</p>
     * @return 结果集
     * @throws DaoException 异常
     */
    @RequestMapping(value="getOverviewData.mvc")
    @ResponseBody
    public Object getOverviewData(){
    	Map<String, Object> dataMap = new HashMap<String, Object>();
    	try {
			dataMap = this.monitorWorkBenchService.getOverviewData();
		} catch (DaoException e) {
			logger.error("获取概览页面数据查询出错：", e);
		}
    	return dataMap;
    }
    
    /**
     * 
     * <p>Description: 获取概览页面需要数据</p>
     * @return 结果集
     * @throws DaoException 异常
     */
    @RequestMapping(value="getAlertClassifyBarChartData.mvc")
    @ResponseBody
    public Object getAlertClassifyBarChartData(){
    	Map<String, Object> dataMap = new HashMap<String, Object>();
    	try {
			dataMap = this.monitorWorkBenchService.getAlertClassifyBarChartData();
		} catch (DaoException e) {
			logger.error("获取概览页面数据查询出错：", e);
		}
    	return dataMap;
    }
    
    /**
     * 
     * <p>Description: 获取概览页面需要数据</p>
     * @return 结果集
     * @throws DaoException 异常
     */
    @RequestMapping(value="getPowerOnDistributeData.mvc")
    @ResponseBody
    public Object getPowerOnDistributeData(){
    	Map<String, Object> dataMap = new HashMap<String, Object>();
    	try {
			dataMap = this.monitorWorkBenchService.getPowerOnDistributeData();
		} catch (DaoException e) {
			logger.error("获取概览页面数据查询出错：", e);
		}
    	return dataMap;
    }
    
    /**
     * 
     * <p>Description: 获取概览页面需要数据</p>
     * @return 结果集
     * @throws DaoException 异常
     */
    @RequestMapping(value="getMaterialTypeData.mvc")
    @ResponseBody
    public Object getMaterialTypeData(){
    	Map<String, Object> dataMap = new HashMap<String, Object>();
    	try {
			dataMap = this.monitorWorkBenchService.getMaterialTypeData();
		} catch (DaoException e) {
			logger.error("获取概览页面数据查询出错：", e);
		}
    	return dataMap;
    }
    
    /**
     * 
     * <p>Description: 获取概览页面需要数据</p>
     * @return 结果集
     * @throws DaoException 异常
     */
    @RequestMapping(value="getPackModelData.mvc")
    @ResponseBody
    public Object getPackModelData(){
    	Map<String, Object> dataMap = new HashMap<String, Object>();
    	try {
			dataMap = this.monitorWorkBenchService.getPackModelData();
		} catch (DaoException e) {
			logger.error("获取概览页面数据查询出错：", e);
		}
    	return dataMap;
    }
    
    /**
     * 
     * <p>Description: 获取概览页面需要数据</p>
     * @return 结果集
     * @throws DaoException 异常
     */
    @RequestMapping(value="getAppticationData.mvc")
    @ResponseBody
    public Object getAppticationData(){
    	Map<String, Object> dataMap = new HashMap<String, Object>();
    	try {
			dataMap = this.monitorWorkBenchService.getAppticationData();
		} catch (DaoException e) {
			logger.error("获取概览页面数据查询出错：", e);
		}
    	return dataMap;
    }
    
    /**
     * 
     * <p>Description: 获取概览页面需要数据</p>
     * @return 结果集
     * @throws DaoException 异常
     */
    @RequestMapping(value="getAppticationDetailData.mvc")
    @ResponseBody
    public Object getAppticationDetailData(){
    	Map<String, Object> dataMap = new HashMap<String, Object>();
    	try {
			dataMap = this.monitorWorkBenchService.getAppticationDetailData();
		} catch (DaoException e) {
			logger.error("获取概览页面数据查询出错：", e);
		}
    	return dataMap;
    }
    
    /**
     * 
     * <p>Description: 解析经纬度</p>
     * @return 结果集
     * @throws DaoException 异常
     */
    @RequestMapping(value="decodeAddress.mvc")
    @ResponseBody
    public Object decodeAddress(@RequestParam("lng")double lng, @RequestParam("lat")double lat){
    	Map<String, Object> dataMap = new HashMap<String, Object>();
    	try {
    		double[] JWD = BaiduAPI.wgs2bd(lat, lng);
			dataMap.put("address", BaiduAPI.DecodeLonLat(JWD[0], JWD[1]));
		} catch (IOException e) {
			logger.error("解析地理位置失败:", e);
		}
    	return dataMap;
    }
}
